package com.example.myapplication5.ui.screens

import android.annotation.SuppressLint
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.myapplication5.ui.components.TodoItem
import com.example.myapplication5.viewmodel.ListViewModel
import com.example.myapplication5.viewmodel.factory.TodoViewModelFactory

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ListScreen(onNavigateToEditor: (String) -> Unit) {

    val context = LocalContext.current
    //ViewModel 需要用到 context 如果通过实例化 ViewModel 传入 LocalContext.current 的话会导致 state 失效
    //解决办法 用AndroidViewModel 里面的 application.applicationContext()
    //通过 ViewModelProvider.Factory 构造 ViewModel
    val listViewModel: ListViewModel = viewModel(factory = TodoViewModelFactory(context))

    LaunchedEffect(key1 = Unit) {
        listViewModel.fetchList()
    }
    val toast = listViewModel.toast
    val lifecycleOwner = LocalLifecycleOwner.current
    LaunchedEffect(key1 = toast, key2 = lifecycleOwner) {
        //设置只有在Start 生命周期才执行
        lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
            toast.collect {
                Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
            }
        }


    }
    Scaffold(topBar = { TopAppBar(title = { Text("List") }) }, floatingActionButton = {
        FloatingActionButton(
            onClick = { onNavigateToEditor("") }) {
            Icon(imageVector = Icons.Default.Add, contentDescription = null)
        }
    }) {
        if (listViewModel.loading) {
            Column(
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                CircularProgressIndicator()
            }
        } else {
            if (listViewModel.list.isEmpty()) {
                Column(
                    modifier = Modifier.fillMaxSize(),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    Text(text = "暂时")
                }
            } else {
                LazyColumn(modifier = Modifier.padding(top = it.calculateTopPadding())) {
                    items(listViewModel.list) { item ->
                        TodoItem(todo = item, onCheckedChange = {
                            Log.e("Tag", "ssss   $it")
                            listViewModel.done(item, it)
                        }, modifier = Modifier.clickable { onNavigateToEditor(item.id) })
                    }
                }
            }
        }
    }
}